Golang SDK
This guide shows you how to integrate the S3 compatible storage layer using Go SDK. We will be using the [minio-go] (https://github.com/minio/minio-go) client to interact with the S3 compatible APIs.
Prerequisites
- You need to have Go installed on your system. You can follow the instructions from here.
- You need to have an access key ID and secret access key. You can create these keys from the dashboard.
Install minio-go
You need to install the minio-go client on your system.
go get github.com/minio/minio-go/v7
Operations
Create a client
package main
import (
"fmt"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
func main() {
// Initialize minio client object.
s3Client, err := minio.New("https://buckets.chainsafe.io", &minio.Options{
Creds: credentials.NewStaticV4("YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", ""),
Secure: true,
})
if err != nil {
fmt.Println(err)
return
}
}
Create a bucket
Creates a new bucket.
// Create a bucket at region 'us-east-1' with object locking enabled.
err = minioClient.MakeBucket(context.Background(), "mybucket", minio.MakeBucketOptions{Region: "us-east-1", ObjectLocking: true})
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Successfully created mybucket.")
List buckets
Lists all buckets.
buckets, err := minioClient.ListBuckets(context.Background())
if err != nil {
fmt.Println(err)
return
}
for _, bucket := range buckets {
fmt.Println(bucket)
}
Bucket Exists
Checks if a bucket exists and you have permission to access it.
found, err := minioClient.BucketExists(context.Background(), "mybucket")
if err != nil {
fmt.Println(err)
return
}
if found {
fmt.Println("Bucket found")
}
Remove a bucket
Removes a bucket.
err = minioClient.RemoveBucket(context.Background(), "mybucket")
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Successfully removed mybucket.")
List Objects
Lists all objects in a bucket.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
objectCh := minioClient.ListObjects(ctx, "mybucket", minio.ListObjectsOptions{
Prefix: "myprefix",
Recursive: true,
})
for object := range objectCh {
if object.Err != nil {
fmt.Println(object.Err)
return
}
fmt.Println(object)
}
Get object
Gets an object from a bucket.
object, err := minioClient.GetObject(context.Background(), "mybucket", "myobject", minio.GetObjectOptions{})
if err != nil {
fmt.Println(err)
return
}
defer object.Close()
localFile, err := os.Create("/tmp/local-file.jpg")
if err != nil {
fmt.Println(err)
return
}
defer localFile.Close()
if _, err = io.Copy(localFile, object); err != nil {
fmt.Println(err)
return
}
Put object
Puts an object in a bucket.
file, err := os.Open("my-testfile")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
fileStat, err := file.Stat()
if err != nil {
fmt.Println(err)
return
}
uploadInfo, err := minioClient.PutObject(context.Background(), "mybucket", "myobject", file, fileStat.Size(), minio.PutObjectOptions{ContentType:"application/octet-stream"})
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Successfully uploaded bytes: ", uploadInfo)
Object Exist
Checks if an object exists in a bucket.
objInfo, err := minioClient.StatObject(context.Background(), "mybucket", "myobject", minio.StatObjectOptions{})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(objInfo)
Copy object
Copies an object from source bucket to destination bucket.
srcOpts := minio.CopySrcOptions{
Bucket: "my-sourcebucketname",
Object: "my-sourceobjectname",
}
// Destination object
dstOpts := minio.CopyDestOptions{
Bucket: "my-bucketname",
Object: "my-objectname",
}
// Copy object call
uploadInfo, err := minioClient.CopyObject(context.Background(), dst, src)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Successfully copied object:", uploadInfo)
Remove object
Removes an object from a bucket.
bucketName := "mybucket"
document1Key := "myobject"
err = client.RemoveObject(ctx, bucketName, document1Key, minio.RemoveObjectOptions{})
if err != nil {
fmt.Println("error deleting the bucket object: ", err.Error())
}
NOTE:
We only support a limited set of S3 functionalities as of now but we intend to expand these functionalities in the coming months. If there is a specific functionality that you would like to request, please email colin@chainsafe.io. You need to set buckets.chainsafe.io:443 endpoint to your library of choice. You need to set the region for s3 client library to us-east-1